package net.caixiaomi.info.ui.discovery;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONObject;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.ActiveItemAdapter;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.ActiveEntity;
import net.caixiaomi.info.model.ActiveListEntity;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.DiscoveryHomePageEntity;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.ui.matching.MatchItemFragment;
import net.caixiaomi.info.util.SchemeUtil;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

import static android.R.id.list;

/**
 * Created by mac on 2018/9/5.
 */

public class ActiveFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, OnRefreshListener,ComonFragmentImpl {

    @BindView(list)
    RecyclerView mListView;
    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;

    private ActiveItemAdapter mAdapter;

    private int mType;

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        loadData();
    }

    public static ActiveFragment newInstance() {
        return new ActiveFragment();
    }

    @Override
    protected int fragmentLayout() {
        return R.layout.match_item_ftag_layout;
    }

    public void setType(int type){
        mType=type;
    }

    @Override
    public void setFilter(String filter) {

    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        mRefresh.setOnRefreshListener(this);
        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter=new ActiveItemAdapter(R.layout.active_item_layout);
        mAdapter.setOnItemClickListener(this);
        mListView.setAdapter(mAdapter);
        loadData();
        mAdapter.setType(mType);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if(mType==0) {
            ActiveEntity entity = (ActiveEntity) adapter.getData().get(position);
            SchemeUtil.parseIntentUrl(getActivity(), entity.getBannerLink(), "");
        }
    }

    private void loadData(){
        JSONObject params = new JSONObject();
        params.put("emptyStr", "");
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<ActiveListEntity>> dis = requestService.getDiscoveryActive(params);
        dis.enqueue(new ResponseCallback<BaseCallModel<ActiveListEntity>>() {
            @Override
            public void onSuccess(BaseCallModel<ActiveListEntity> data) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                ActiveListEntity entity=data.data;
                List<ActiveEntity> list=null;
                if(mType==0){
                    list=entity.getOnlineList();
                }else{
                    list=entity.getOfflineList();
                }

                List<MultiItemEntity> lList=new ArrayList<>();
                lList.addAll(list);
                mAdapter.setNewData(lList);
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }
        });
    }


}
